using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using RPGOnline.Extensions;
using RPGOnline.Rendering;

namespace RPGOnline.Controls.Map
{
    public class MapCellFrame
    {
        private readonly Point m_topLeftAngle;
        private readonly MapTileset m_tileset;
        private readonly Texture m_texture;
        private int m_textureWidth;
        private int m_textureHeight;
	    private float m_onePixelInWidth;
	    private float m_onePixelInHeight;

	    public MapCellFrame(Point topLeftAngle, MapTileset tileset, Texture texture)
        {
            m_topLeftAngle = topLeftAngle;
            m_tileset = tileset;
            m_texture = texture;

            SurfaceDescription surfaceDescription = m_texture.GetLevelDescription(0);
            m_textureWidth = surfaceDescription.Width;
            m_textureHeight = surfaceDescription.Height;
			m_onePixelInWidth = 1.0f / m_textureWidth;
			m_onePixelInHeight = 1.0f / m_textureHeight;
        }

		//private CustomVertex.TransformedColoredTextured[] GetVertices(int cellX, int cellY, int alpha)
		//{
		//    var vertices = new CustomVertex.TransformedColoredTextured[4];
		//    float onePixelInWidth = 1.0f/m_textureWidth;
		//    float onePixelInHeight = 1.0f/m_textureHeight;

		//    vertices[0].Position = new Vector4(cellX * m_tileset.TileSize.Width, cellY * m_tileset.TileSize.Height, 0, 1.0f);
		//    vertices[0].Color = Color.FromArgb(alpha, Color.White).ToArgb();
		//    vertices[0].Tu = m_topLeftAngle.X * onePixelInWidth;
		//    vertices[0].Tv = m_topLeftAngle.Y * onePixelInHeight;

		//    vertices[1].Position = new Vector4(cellX * m_tileset.TileSize.Width + m_tileset.TileSize.Width, cellY * m_tileset.TileSize.Height, 0, 1.0f);
		//    vertices[1].Color = Color.FromArgb(alpha, Color.White).ToArgb();
		//    vertices[1].Tu = (m_topLeftAngle.X + m_tileset.TileSize.Width) * onePixelInWidth;
		//    vertices[1].Tv = m_topLeftAngle.Y * onePixelInHeight;

		//    vertices[2].Position = new Vector4(cellX * m_tileset.TileSize.Width, cellY * m_tileset.TileSize.Height + m_tileset.TileSize.Height, 0, 1.0f);
		//    vertices[2].Color = Color.FromArgb(alpha, Color.White).ToArgb();
		//    vertices[2].Tu = m_topLeftAngle.X * onePixelInWidth;
		//    vertices[2].Tv = (m_topLeftAngle.Y + m_tileset.TileSize.Height) * onePixelInHeight;

		//    vertices[3].Position = new Vector4(cellX * m_tileset.TileSize.Width + m_tileset.TileSize.Width, cellY * m_tileset.TileSize.Height + m_tileset.TileSize.Height, 0, 1.0f);
		//    vertices[3].Color = Color.FromArgb(alpha, Color.White).ToArgb();
		//    vertices[3].Tu = (m_topLeftAngle.X + m_tileset.TileSize.Width) * onePixelInWidth;
		//    vertices[3].Tv = (m_topLeftAngle.Y + m_tileset.TileSize.Height) * onePixelInHeight;
		//    return vertices;
		//}

        public void Draw(Renderer renderer, int cellX, int cellY, bool withTexture = true, int alpha = 255)
        {
			Matrix renderMatrix = Matrix.Scaling(m_tileset.TileSize.Width, m_tileset.TileSize.Height, 1) * Matrix.Translation(cellX * m_tileset.TileSize.Width, cellY * m_tileset.TileSize.Height, 0);

	        if (withTexture)
	        {
		        Matrix textureMatrix = 
					Matrix.Scaling(m_tileset.TileSize.Width*m_onePixelInWidth, m_tileset.TileSize.Height*m_onePixelInHeight, 1)*
					TextureMatrix.Translate2D(m_topLeftAngle.X * m_onePixelInWidth, m_topLeftAngle.Y * m_onePixelInHeight);
				
		        renderer.DrawQuad(renderMatrix, textureMatrix, m_texture);
	        }
	        else
		        renderer.DrawQuad(renderMatrix);

			//TODO: Reimplement alpha blending, possibly in a simple shader

			//device.SetTexture(0, m_texture);
			//device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
			//device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, GetVertices(cellX, cellY, alpha));
        }
    }
}